package com.example.demo.controller;

import com.example.demo.entity.User2;
import com.example.demo.util.LoginInfoUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

/**
 * @author 何景航
 * @date 2025/3/4 14:09
 */
@Controller
public class UserController {
    @RequestMapping(value = "/")
    @ResponseBody
    public String index(){
        return "Index";
    }
    @RequestMapping(value = "/toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping(value = "/welcome")
    @ResponseBody
    public Object welcome(Principal principal){
        return principal;
    }
    @RequestMapping(value = "/welcome2")
    @ResponseBody
    public Object welcome2(Authentication authentication){
        return authentication;
    }
    @RequestMapping(value = "/welcome3")
    @ResponseBody
    public Object welcome3(UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken){
        return usernamePasswordAuthenticationToken;
    }
    @RequestMapping(value = "/welcome4")
    @ResponseBody
    public Object welcome4(){
        return SecurityContextHolder.getContext().getAuthentication();
    }
    @RequestMapping(value = "/welcome5")
    @ResponseBody
    public Object welcome5(){
        return LoginInfoUtil.getCurrentLoginUser();
    }
    @RequestMapping(value = "/test")
    @ResponseBody
    public Object test(Principal principal){
        return new User2();
    }
}
